/**
 * The StripFilter will strip measurements from the incoming pipe
 * and send those measurements to the output pipe 
 * 
 * This filter handles the data stream by using utility methods
 * to read and write Frame objects
 * 
 * @author Lorena Vazquez
 */
package edu.cmu.arch.pipesandfilters;

import java.io.IOException;
import java.util.ArrayList;
import edu.cmu.arch.pipesandfilters.frame.Frame;
import edu.cmu.arch.pipesandfilters.frame.Variable;

public class StripFilter extends FrameFilterFramework {
	
	
	ArrayList<Variable> measurements = new ArrayList<Variable>();//List of measurements that will sent out to the output port

	public StripFilter(ArrayList<Variable> variablesToRetain) {
		measurements = variablesToRetain;//assign to local variable
	}

	/**
	 * (non-Javadoc)
	 * @see edu.cmu.arch.pipesandfilters.FilterFramework#run()
	 */
	@Override
	public void run() {

		Frame inFrame = null;//from incoming stream
		Frame outFrame = null;//for sending out to stream
		System.out.print("\n" + this.getName() + "::StripFilter Reading ");
		try {
				while (true) {
				
				/*
				 * Read frame and retrieve measurements that will be
				 * placed on new Frame and sent to the output pipe
				 */		
				inFrame = readFrame();
				outFrame = new Frame();
				for (int i = 0; i < measurements.size(); i++) {

					outFrame.putValue(measurements.get(i),
							inFrame.getValue(measurements.get(i)));

				}
				writeFrame(outFrame);
				}
			}
			catch (IOException ioe) {
				System.out.println("IOException while reading input stream in StripFilter:  "+ ioe.getMessage());
			} catch (EndOfStreamException e) {
				System.out.println("End of stream caught");
			} finally {
				ClosePorts();
				System.out.print("\n" + this.getName()+ "::StripFilter Exiting");
			}
		

	}
	
}
